import java.util.Random;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {

        int l, c; // l - lines; c - columns

        /* Input data */
        Scanner scanner = new Scanner(System.in);
        System.out.print("Input 'l' (lines number): ");
        l = scanner.nextInt();

        System.out.print("Input 'c' (columns number): ");
        c = scanner.nextInt();

        /* Generate two-additional array */
        // Create array and "random object"
        int[][] arrayNum = new int[l][c];
        Random randomNum = new Random();

        // Filling array
        for (int i = 0; i < l; i++) {
            for (int j = 0; j < c; j++) {
                arrayNum[i][j] = randomNum.nextInt(0, 99);
            }
        }

        System.out.println("\nDisplay matrix:");
        DisplayMatrix(arrayNum, l, c);

        /* Sorting */
        SortMethod(arrayNum, l, c);
        System.out.println("\nDisplay sorted matrix:");
        DisplayMatrix(arrayNum, l, c);
    }

    public static void SortMethod(int[][] array, int l, int c) {

        int t, akkum = 1;   // t - temp variable; akkum - akkumulate variable

        while (akkum < array.length) {
            // j - column; i - line
            for (int j = 0; j < c; j++) {           // column
                for (int i = 0; i < l - 1; i++) {   // line
                    if (array[i][j] > array[i + 1][j]) {
                        t = array[i][j];
                        array[i][j] = array[i + 1][j];
                        array[i + 1][j] = t;
                    }
                }
            }
            akkum++;
        }

    }


    public static void DisplayMatrix(int[][] array, int l, int c) {

        for (int i = 0; i < l; i++) {
            for (int j = 0; j < c; j++) {
                System.out.print(array[i][j] + "\t");
            }
            System.out.println();
        }

    }
}